/**
 * 
 */
package gestionBulletins.view;

import gestionBulletins.controller.MyTableModel;
import gestionBulletins.model.persistance.DaoFactory;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * @author Paraita Wohler
 *
 *
 *
 */
public class MainWindow extends JFrame implements ActionListener {
	
	private JButton boutonAjouter;
	private JButton boutonConsulter;
	private JButton boutonSave;
	private JButton boutonGererSections;
	private JButton boutonExit;
	public InsertWindow insertWindow;
	public ConsultWindow consultWindow;
	public InfosSectionWindow infosSectionWindow;
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MainWindow(DaoFactory daoFactory) {
		super();
		//this.setSize(600, 400);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setTitle("Gestion de bulletins - Menu");
		
		/* instanciation des boutons */
		boutonAjouter = new JButton("Ajouter bulletins");
		boutonAjouter.addActionListener(this);
		boutonConsulter = new JButton("Consulter base de données");
		boutonConsulter.addActionListener(this);
		boutonSave = new JButton("Sauvegarder");
		boutonSave.addActionListener(this);
		boutonGererSections = new JButton("Ajouter/supprimer des sections");
		boutonGererSections.addActionListener(this);
		boutonExit = new JButton("Quitter l'application");
		boutonExit.addActionListener(this);
		
		/* instanciation des autres fenetres */
		MyTableModel tableModel = new MyTableModel(daoFactory);
		consultWindow = new ConsultWindow(tableModel);
		tableModel.setVue(consultWindow);
		insertWindow = new InsertWindow(daoFactory);
		insertWindow.addObserver(tableModel);
		infosSectionWindow = new InfosSectionWindow(daoFactory, this);
		
		/* on assemble la fenetre principale */
		Container panel = getContentPane();
		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		panel.add(Box.createRigidArea(new Dimension(250, 5)));
		panel.add(boutonAjouter);
		panel.add(Box.createRigidArea(new Dimension(250, 5)));
		panel.add(boutonConsulter);
		panel.add(Box.createRigidArea(new Dimension(250, 5)));
		panel.add(boutonGererSections);
		panel.add(Box.createRigidArea(new Dimension(250, 5)));
		panel.add(boutonSave);
		panel.add(Box.createRigidArea(new Dimension(250, 5)));
		panel.add(boutonExit);
		boutonAjouter.setAlignmentX(Container.CENTER_ALIGNMENT);
		boutonConsulter.setAlignmentX(Container.CENTER_ALIGNMENT);
		boutonSave.setAlignmentX(Container.CENTER_ALIGNMENT);
		boutonGererSections.setAlignmentX(Container.CENTER_ALIGNMENT);
		boutonExit.setAlignmentX(Container.CENTER_ALIGNMENT);
		getRootPane().setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
		/* degueu mais j'ai pas trouvé comment faire autrement :/ */
		boutonAjouter.setMinimumSize(new Dimension(300, 25));
		boutonAjouter.setMaximumSize(new Dimension(300, 25));
		boutonAjouter.setPreferredSize(new Dimension(300, 25));
		boutonConsulter.setMinimumSize(new Dimension(300, 25));
		boutonConsulter.setMaximumSize(new Dimension(300, 25));
		boutonConsulter.setPreferredSize(new Dimension(300, 25));
		boutonSave.setMinimumSize(new Dimension(300, 25));
		boutonSave.setMaximumSize(new Dimension(300, 25));
		boutonSave.setPreferredSize(new Dimension(300, 25));
		boutonGererSections.setMinimumSize(new Dimension(300, 25));
		boutonGererSections.setMaximumSize(new Dimension(300, 25));
		boutonGererSections.setPreferredSize(new Dimension(300, 25));
		boutonExit.setMinimumSize(new Dimension(300, 25));
		boutonExit.setMaximumSize(new Dimension(300, 25));
		boutonExit.setPreferredSize(new Dimension(300, 25));
		
		/* pack */
		pack();
		
		/* position au milieu de l'écran */
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation((dim.width - getSize().width) / 2,
						 (dim.height - getSize().height) / 2);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == boutonAjouter) {
			insertWindow.setVisible(true);
		}
		if (e.getSource() == boutonConsulter) {
			consultWindow.setVisible(true);
		}
		if (e.getSource() == boutonSave) {
			JFileChooser jc = new JFileChooser();
			int shouldSave = jc.showSaveDialog(this);
			if (shouldSave == JFileChooser.APPROVE_OPTION) {
				String path = jc.getSelectedFile().getAbsolutePath();
				File fSource = new File("bdd.sqlite");
				File fDest = new File(path);
				boolean copyOk = true;
				/* si le dest n'existe pas je le touch */
				if (!fDest.exists()) {
					try {
						fDest.createNewFile();
					}
					catch (IOException e1) {
						e1.printStackTrace();
						copyOk = false;
					}
				}
				FileChannel source = null;
				FileChannel dest = null;
				try {
					source = new FileInputStream(fSource).getChannel();
					dest = new FileOutputStream(fDest).getChannel();
					dest.transferFrom(source, 0, source.size());
				}
				catch(Exception euh) {
					euh.printStackTrace();
					copyOk = false;
				}
				finally {
					try {
						if (source != null) {
							source.close();
						}
						if (dest != null) {
							dest.close();
						}
					}
					catch(IOException ioe) {
						ioe.printStackTrace();
						copyOk = false;
					}
				}
				if (copyOk) {
					JOptionPane.showMessageDialog(this, "La sauvegarde s'est bien déroulée !");
				}
				else {
					JOptionPane.showMessageDialog(this, "Erreur lors de la sauvegarde ! Faut appeller Ninipou !");
				}
			}
			
		}
		if (e.getSource() == boutonGererSections) {
			infosSectionWindow.setVisible(true);
		}
		if (e.getSource() == boutonExit) {
			System.exit(0);
		}
	}
	
	/* pour test séparé */
	public static void main(String[] args) {
		MainWindow main = new MainWindow(null);
		main.setVisible(true);
	}
}
